<?php
/**
 * SuReplace_1_0
 * @since 1.0 <2015-6-25> SoChishun Added.
 */
$ver = '1.0';
$config = array(
    'XCall_5_0' => array(
        'default_replaces' => array('<ul>' => '<table>', '</ul>' => '</table>', '<li' => '<tr', '</li>' => '</td></tr>', '<label' => '<th', '</label>' => '</th><td>', '<form ' => '<extend name="Layout:base_edit" /><block name="body_content_block"><form ', '</form>' => '</form></block>', '<script' => '<block name="body_after_block"><script', '</script>' => '</script></block>', 'type="text"' => 'type="text" class="easyui-textbox"'),
        'F:\PDC\ProjectEnv\xcall150505\Application\XCall_5_0\View\default\*' => array(
            'rules' => array('recursive' => true, 'allow_files' => array('*_edit.html', 'add_number.html'), 'disable_files' => array('*_list.html', 'index.html')),
            'replaces' => array('<ul>' => '<table>', '</ul>' => '</table>', '<li' => '<tr', '</li>' => '</td></tr>', '<label' => '<th', '</label>' => '</th><td>', '<form ' => '<extend name="Layout:base_edit" /><block name="body_content_block"><form ', '</form>' => '</form></block>', '<script' => '<block name="body_after_block"><script', '</script>' => '</script></block>', 'type="text"' => 'type="text" class="easyui-textbox"'),
        ),
    ),
);

$replaces = fn_request('replaces');
$path = fn_request('path');
$id = fn_request('id');
$msg = '';
if ($id) {
    if (!$replaces) {
        $arr_replaces = $config[$id]['default_replaces'];
        foreach ($arr_replaces as $key => $value) {
            $replaces.=PHP_EOL . $key . '=>' . $value;
        }
    }
}
if ($path && is_file($path)) {
    $arr_replaces = explode(PHP_EOL, $replaces);
    $str = file_get_contents($path);
    foreach ($arr_replaces as $row) {
        if (!$row || false === strpos($row, '=>')) {
            continue;
        }
        $arr = explode('=>', $row);
        $str = str_replace($arr[0], $arr[1], $str);
    }
    file_put_contents($path, $str);
    $msg = '<strong class="success">操作成功</strong>';
}

/**
 * 获取浏览器传递的参数值
 * @param string $name
 * @param string $defv
 * @return string
 * @since 1.0 <2015-6-2> SoChishun Added.
 */
function fn_request($name, $defv = '') {
    $v = false;
    if (isset($_POST[$name])) {
        $v = $_POST[$name];
    }
    if (false === $v && isset($_GET[$name])) {
        $v = $_GET[$name];
    }
    return false === $v ? $defv : $v;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>SuReplace <?php echo $ver; ?></title>
        <style type="text/css">
            html { margin:0px; font-size:12px;}
            .error { color:#F00; margin:5px 0px; font-weight: bold;}
            .success{ color:#090; margin:5px 0px; font-weight: bold;}
            table { border:solid 1px #CCC; border-right: none; border-bottom: none;}
            th, td { border:solid 1px #CCC; border-top:none; border-left:none; padding:5px; line-height: 18px;}
            td .error { font-weight:normal;}
        </style>
    </head>
    <body>
        <h1>SuReplace 批量替换工具</h1>
        <form method="get" action="SuReplace_1_0.php">
            <?php
            echo '选择项目：<select name="id" onchange="change_id(this)"><option>==选择==</option>';
            $keys = array_keys($config);
            foreach ($keys as $key) {
                echo '<option value="', $key, '"', ($id == $key ? ' selected="selected"' : ''), '>', $key, '</option>';
            }
            echo '</select>';
            ?>
            <input type="submit" value="切换" />
        </form>
        <div><?php echo $msg; ?></div>
        <form method="post" action="SuReplace_1_0.php?id=<?php echo $id; ?>">
            <table>
                <tr><th>替换内容：</th><td><textarea name="replaces" cols="90" rows="6"><?php echo $replaces; ?></textarea></td></tr>
                <tr><th>文件路径：</th><td><input type="text" name="path" size="103" value="<?php echo $path; ?>" /></td></tr>
                <tr><td colspan="2"><input type="submit" value="替换" /></td></tr>
            </table>
        </form>
        <h2>读取并格式化</h2>
        <?php
        $path2 = fn_request('path2');
        $str = '';
        $scope = '';
        if ($path2) {
            $str = file_get_contents($path2);
            preg_match_all('/<th.*?>(.*?)<\/th>/', $str, $arr_ths);
            preg_match_all('/<td.*?>(.*?)<\/td>/', $str, $arr_tds);
            $str = '';
            if ($arr_ths) {
                $i = 0;
                $ths = $arr_ths[1];
                $tds = $arr_tds[1];
                $list_fields = '';
                $export_fields = '';
                $n_tds=count($tds);
                foreach ($ths as $th) {
                    if ($th == '序号' || $th == '操作') {
                        $i++;
                        continue;
                    }
                    if($i>=$n_tds){
                        break;
                    }
                    $td = $tds[$i];
                    if (false !== ($pos = strpos($td, '.'))) {
                        $td = substr($td, $pos + 1, -1);
                    }
                    $has_fn = '';
                    if (false !== ($pos = strpos($td, '|'))) {
                        $td = substr($td, 0, $pos);                        
                        $has_fn = 'Text';
                    }
                    $str .= '{title: "' . $th . '", field: "' . $td . $has_fn . '", sortable: true},' . PHP_EOL;
                    $list_fields.=',' . $td;
                    $export_fields.=",$td as '$th'";
                    $i++;
                }
                $scope = '\'export\' => array(' . PHP_EOL . '\'field\' => "' . substr($export_fields, 1) . '",' . PHP_EOL . '\'order\' => \'siteID\',' . PHP_EOL . '\'cache\' => true,' . PHP_EOL . '),' . PHP_EOL;
                $scope .= '\'item_list\' => array(' . PHP_EOL . '\'field\' => \'' . substr($list_fields, 1) . '\',' . PHP_EOL . '\'order\' => \'\',' . PHP_EOL . '),';
            }
        }
        ?>
        <form method="post" action="SuReplace_1_0.php">
            <table>
                <tr><th>文件路径：</th><td><input type="text" name="path2" size="103" value="<?php echo $path2; ?>" /></td>
                <tr><th>格式结果：</th><td><textarea cols="90" rows="9"><?php echo $str; ?></textarea></td></tr>
<tr><th>item_list</th><td><textarea cols="90" rows="4"><?php echo $scope; ?></textarea></td></tr>
                <tr><td colspan="2"><input type="submit" value="读取" /></td></tr>
            </table>            
        </form>
    </body>
</html>
